/*
I needed to overload the date object to get some functionality from it
*/
Date.prototype.subtractDate = function( subtrahend ) {
	difference = new Date();
	difference.setTime(Math.abs(this.getTime() - subtrahend.getTime()));
	return difference;	
}

Date.prototype.addDate = function( addend ) {
	summand  = new Date();
	summand.setTime(Math.abs(this.getTime() - addends.getTime()));
	return summand;
}

Date.prototype.addByPartName = function(strPart, addend ) {

	switch(strPart)
	{
		case 'day':
			this.setDate(this.getDate() + addend);
			break;
		case 'month':
			this.setMonth(this.getMonth() + addend);
			break;
		case 'year':
			this.setFullYear(this.getFullYear() + addend);
			break;
		case 'hour':
			this.setHours(this.getHours() + addend);
			break;
		case 'minute':
			this.setMinutes(this.getMinutes() + addend);
			break;
		case 'second':
			this.setSeconds(this.getSeconds() + addend);
			break;
		case 'millisecond':
			this.setMilliseconds(this.getMilliseconds() + addend);
			break;
		default:
			this.setTime(this.getTime() + addend);
	}
}

Date.prototype.getByPartName = function(strPart, bePretty)
{
	switch(strPart)
	{
		case 'day':
			return bePretty ? (100 + this.getDate()).substr(1,2) : this.getDate();
			break;
		case 'weekday':
			return this.getDay();
			break;
		case 'month':
			return bePretty ? this.getMonth() + 1 : this.getMonth();
			break;
		case 'year':
			return this.getFullYear(); // this.getFullYear().toString().substr(2,2)
			break;
		case 'decade':
			return Math.floor(this.getFullYear() / 10) + '0s';
			break;
		case 'century':
			return Math.floor(this.getFullYear() / 100) + '00';
			break;
		case 'millenium':
			return Math.floor(this.getFullYear() / 1000)+ '000';
			break;
		case 'hour':
			return this.getHours();
			break;
		case 'minute':
			return this.getMinutes();
			break;
		case 'second':
			return this.getSeconds();
			break;
		case 'millisecond':
			return this.getMilliseconds();
			break;
		default:
			return this.getTime();
	}
};

Date.prototype.setByPartName = function(strPart, value)
{
	switch(strPart)
	{
		case 'day':
			this.setDate(value);
			break;
		case 'month':
			this.setMonth(value);
			break;
		case 'year':
			this.setFullYear(value);
			break;
/*
		case 'decade':
			this.getFullYear() / 10;
			break;
		case 'century':
			this.getFullYear() / 100;
			break;
		case 'millenium':
			this.getFullYear() / 1000;
			break;
*/
		case 'hour':
			this.setHours(value);
			break;
		case 'minute':
			this.setMinutes(value);
			break;
		case 'second':
			this.setSeconds(value);
			break;
		case 'millisecond':
			this.setMilliseconds(value);
			break;
		default:
			this.setTime(value);
	}
};

// if bTotal, returns the total parts count since 1970
// else returns the count within the current dates context 
Date.prototype.getByPartNameCount = function(strPart)
{
	num = this.getTime();
	switch(strPart)
	{
		case 's':
			return Math.floor(num / (1000));
			break;
		case 'min':
			return Math.floor(num / (1000 * 60));
			break;
		case 'h':
			return Math.floor(num / (1000 * 60 * 60));
			break;
		case 'day':
			return Math.floor(num / (1000 * 60 * 60 * 24));
			break;
		case 'week':
			return Math.floor(num / (1000 * 60 * 60 * 24 * 7));
			break;
		case 'month':
			// 30.4375 = average month length (365.25/12)... is this a bs way to do it?  I'll keep my eye out for a better way
			return Math.floor(num / (1000 * 60 * 60 * 24 * 30.4375)); 
			break;
		case 'year':
			return Math.floor(num / (1000 * 60 * 60 * 24 * 365.25));
			break;
		case 'decade':
			return Math.floor(num / (1000 * 60 * 60 * 24 * 365.25 * 10));
			break;
		case 'century':
			return Math.floor(num / (1000 * 60 * 60 * 24 * 365.25 * 100));
			break;
		case 'millenium':
			return Math.floor(num / (1000 * 60 * 60 * 24 * 365.25 * 1000));
			break;
		default:
			return this.getTime();
	}
};


function daysInMonth(iMonth, iYear)
{
	return 32 - new Date(iYear, iMonth, 32).getDate();
}

function allparts()
{

	timediff = diff.getTime();

	weeks = Math.floor(timediff / (1000 * 60 * 60 * 24 * 7));
	timediff -= weeks * (1000 * 60 * 60 * 24 * 7);
	
	days = Math.floor(timediff / (1000 * 60 * 60 * 24)); 
	timediff -= days * (1000 * 60 * 60 * 24);
	
	hours = Math.floor(timediff / (1000 * 60 * 60)); 
	timediff -= hours * (1000 * 60 * 60);
	
	mins = Math.floor(timediff / (1000 * 60)); 
	timediff -= mins * (1000 * 60);
	
	secs = Math.floor(timediff / 1000); 
	timediff -= secs * 1000;
	
	dateform.difference.value = weeks + " weeks, " + days + " days, " + hours + " hours, " + mins + " minutes, and " + secs + " seconds";	
}